package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.FacilityInfoMapper;
import com.atguigu.lease.web.admin.mapper.FeeValueMapper;
import com.atguigu.lease.web.admin.mapper.LabelInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;


    @Override
    public void saveOrUpdateItem(ApartmentSubmitVo apartmentSubmitVo) {
        if(apartmentSubmitVo.getId()!=null){
            //修改
            //1.删除相关图片
            LambdaQueryWrapper<GraphInfo> graphVoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphVoLambdaQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphVoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphVoLambdaQueryWrapper);
            //2.删除公寓列表
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //3.删除标签列表
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            //4/删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
        }

        ProvinceInfo info = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        apartmentSubmitVo.setProvinceName(info.getName());

        CityInfo cityInfoServiceById = cityInfoService.getById(apartmentSubmitVo.getCityId());
        apartmentSubmitVo.setCityName(cityInfoServiceById.getName());

        DistrictInfo districtInfoServiceById = districtInfoService.getById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setDistrictName(districtInfoServiceById.getName());


        //开始新增

        this.saveOrUpdate(apartmentSubmitVo);
//        Long id = apartmentSubmitVo.getId();

        //1.插入图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setName(graphVo.getName());
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfoList.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfoList);

        //2.插入配套列表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
        for (Long facilityInfoId : facilityInfoIds) {
            ApartmentFacility apartmentFacility = ApartmentFacility.builder().build();
            apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
            apartmentFacility.setFacilityId(facilityInfoId);
            apartmentFacilityList.add(apartmentFacility);
        }
        apartmentFacilityService.saveBatch(apartmentFacilityList);

        //3.
        //3.插入标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabelList = new ArrayList<>(labelIds.size());
        for (Long labelId : labelIds) {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder().build();
            apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
            apartmentLabel.setLabelId(labelId);
            apartmentLabelList.add(apartmentLabel);
        }
        apartmentLabelService.saveBatch(apartmentLabelList);

        //4.插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>(feeValueIds.size());
        for (Long feeValueId : feeValueIds) {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().build();
            apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
            apartmentFeeValue.setFeeValueId(feeValueId);
            apartmentFeeValueList.add(apartmentFeeValue);
        }
        apartmentFeeValueService.saveBatch(apartmentFeeValueList);
    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {

        return apartmentInfoMapper.pageItem(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //1.查询公寓的信息
        ApartmentInfo apartmentInfo = this.getById(id);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo );

        //3.获取图片列表
        LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GraphInfo::getItemId,id)
                        .eq(GraphInfo::getItemType, ItemType.APARTMENT);
        List<GraphInfo> graphInfos = graphInfoService.list(queryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>(graphInfos.size());
        for (GraphInfo graphInfo : graphInfos) {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphVoList.add(graphVo);
        }
        apartmentDetailVo.setGraphVoList(graphVoList);


        //4.获取标签列表

        //第①次写法
//        LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
//        List<ApartmentLabel> apartmentLabels = apartmentLabelService.list(labelLambdaQueryWrapper);
//        List<LabelInfo> labelInfoList = new ArrayList<>();
//        for (ApartmentLabel apartmentLabel : apartmentLabels) {
//            LabelInfo labelInfo = labelInfoService.getById(apartmentLabel.getLabelId());
//            labelInfoList.add(labelInfo);
//        }
//        apartmentDetailVo.setLabelInfoList(labelInfoList);
        //②
        List<LabelInfo> labelInfoList =  labelInfoMapper.selectByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList);

        //5.配套管理

        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);


        //6.杂费
        List<FeeValueVo> feeValueVoList =feeValueMapper.selectByApartmentId(id);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void deletedById(Long id) {
        //删除之前先判断该公寓是否还有房间
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(lambdaQueryWrapper);
        if(count > 0){
            throw new LeaseException(ResultCodeEnum.ADMIN_HAS_ROOM);
        }

        //1.删除相关图片
        LambdaQueryWrapper<GraphInfo> graphVoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphVoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphVoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(graphVoLambdaQueryWrapper);
        //2.删除公寓列表
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        //3.删除标签列表
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        //4/删除杂费列表
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        this.removeById(id);
    }


}




